package meijuli.com.join51.business.adapter;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.adapter.base.ComAdapter;
import meijuli.com.join51.business.adapter.base.ComViewHolder;
import meijuli.com.join51.business.model.ResFlowObj;
import meijuli.com.join51.business.utils.DoubleClick;
import meijuli.com.join51.business.utils.TimeUtil;

import com.tencent.bugly.crashreport.CrashReport;

import java.util.List;

/**
 * 订单详情
 * Created by wenqin on 2017/3/21.
 *
 * @author jack
 */

public class FlowAdapter extends ComAdapter<ResFlowObj> {
    private OnItemClickListener mItemClickListener;
    public FlowAdapter(Context context, List<ResFlowObj> datas) {
        super(context, datas, R.layout.item_flow);
    }

    @Override
    public void convert(ComViewHolder holder, ResFlowObj resFlowObj, final int position) {
        String ordered = resFlowObj.getOrderId();
        String typeMsg = resFlowObj.getTypeMsg();
        holder.setText(R.id.tv_order_id, ordered + "\n" + typeMsg);

        String formatTime = TimeUtil.getFormatTime(resFlowObj.getCtime(), "yyyy-MM-dd HH:mm:ss");
        String[] split = formatTime.split(" ");
        String date = split[0];
        String time = split[1];
        String format = String.format(getContext().getResources().getString(R.string.date),
                date, time);
        holder.setText(R.id.tv_time, Html.fromHtml(format));

        holder.setText(R.id.tv_money, "¥ " + resFlowObj.getStringAmount());

        if (!TextUtils.isEmpty(resFlowObj.getExtractPic())) {
            ImageView imageStamp = holder.getView(R.id.image_flow_stamp);
            Glide.with(mContext).load(resFlowObj.getExtractPic()).error(R.mipmap.img_default).into(imageStamp);
        }

        holder.getView(R.id.view_item).setOnClickListener(new DoubleClick() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(mItemClickListener != null) {
                    mItemClickListener.onItemClick(v, position);
                }
            }
        });

        double money = 0.0000;
        try {
            money = Double.parseDouble(resFlowObj.getStringAmount());
        } catch (Exception e) {
            CrashReport.postCatchedException(new Throwable("FlowAdapter::53 line", e));
        }
        if (money < 0) {
            holder.setTextColor(R.id.tv_money, R.color.text_blue);
        } else {
            holder.setTextColor(R.id.tv_money, R.color.red);
        }
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {
        /**
         * item 点击事件
         * @param view
         * @param position
         */
        void onItemClick(View view, int position);
    }
}
